<?php
/**
 * 《银行联行号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CoreUtil.class.php');

class Table extends CoreUtil {
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';
	
		parent::__construct($request);
	}
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		// '2' => '维修',
		'3' => '暂停',
		'4' => '停止',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'park' => '停放地点',
		'emp' => '司机',
		'buydate' => '购买日期',
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];
	
		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);
	
			return $row;
		}
	
		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
	
		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}
	
		$controller->assign('orderby', $orderby);
	
		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		return "visible = 1";
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
	
		$my = $this->_my();
	
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
	
		if (!empty($name_s)) {
			$where .= " AND name = '" . $name_s . "'";
		}
	
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
	
		return $where;
	}
	
	function tab($controller, $request) {
		$data = array(
			'类库检测' => array('cc'=>'core.php?do=Yang.find', 'js'=>''),
			'编号信息' => array('cc'=>'info.php?do=Code.find', 'js'=>''),
			'部门信息' => array('cc'=>'info.php?do=Dept.find', 'js'=>'1'),
				
			'工作中心' => array('cc'=>'info.php?do=Work.find', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);
		$sql = "SHOW TABLES";
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$data[$row['Tables_in_fy']] = $row['Tables_in_fy'];
		}
		
		// print_r($data);
		
		$controller->assign_by_ref('table_option', $data);
	
		$controller->display($request, 'core/table/find');
		return true;
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);
		
		// print_r($request);
		
		$tbl = $request['table_s'];
	
		$this->findPara($controller, $request);
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $tbl, $where, $orderby);
		debug($sql);
	
		$pager_option = array (
				'db' => $db,
				'sql' => $sql,
				'PageSize' => $this->pagesize,
				'CurrentPageID' => $request['p'],
				'numItems' => $request['n']
		);
	
		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();
	
		if ($pager->number > 0) {
	
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
	
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);
	
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
	
		$controller->display($request, 'core/table/list');
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'core/table/edit');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		clean($this->tbl . "_id_" . $id);
	
		$code = $request['code'];
		$name = $request['name'];
	
		$buydate = $request['buydate'];
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
	
		$park = $request['park'];
		$content = addslashes($request['content']);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', code = '%s', name = '%s', buydate = '%s', supplier = '%s', supplier_x = '%s', park = '%s'";
		$fmt .= ", content = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $code, $name, $buydate, $supplier, $supplier_x, $park, $content, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function update($controller, $request) {
		$this->pretreat($controller);
	
		$db = newdb($this->dsn_r);
		$sql = "SHOW TABLES";
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$data[$row['Tables_in_fy']] = $row['Tables_in_fy'];
		}
		
		// print_r($data);
		
		$controller->assign_by_ref('table_option', $data);
	
		$controller->display($request, 'core/table/update');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function updatePost($controller, $request) {
		$this->pretreat($controller);
		
		$tbl = $request['table'];
	
		$from = $this->_input($request['emp_from']);
		$to = $this->_input($request['emp_to']);
	
		$t1 = $request['createtime1'];
		$t2 = $request['createtime2'];
		
		$t3 = $request['modifytime1'];
		$t4 = $request['modifytime2'];
	
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $tbl, $to, $from);
		if (!empty($t1)) {
			$sql .= sprintf(" AND createtime >= '%s'", $t1);
		}
		
		if (!empty($t2)) {
			$sql .= sprintf(" AND createtime < '%s'", $t2);
		}
		
		if (!empty($t3)) {
			$sql .= sprintf(" AND modifytime >= '%s'", $t3);
		}
		
		if (!empty($t4)) {
			$sql .= sprintf(" AND modifytime < '%s'", $t4);
		}
		
		if (!empty($status)) {
			$sql .= sprintf(" AND status = '%s'", $status);
		}
		
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'core/table/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row['supplier_x'] = json_decode($row['supplier_x']);
	
		$row = $this->_view_t($row);
	
		return $row;
	}
}